* 	File-Name: 		CPS_fig5_mainresults_educ.do
*
*	Data Used:  	JHU covid data; YouGov; Monmouth polls
*					fivethirtyeight; RMG polls; policy data from Raifman et al. (2020)
*
*					Source: https://github.com/KristenNocka/COVID-19-US-State-Policy-Database
*
*	Output Files:	Figure A7
*
*	Purpose:   		.do file for population staggered DiD event study estimation 
*	
*	Authors: 		AX, TI
*
*	************************************************************************

clear all 
cd "/Users/alicezxu/Dropbox/Trump Polls/CPS Replication/Replication_final/Data/"
use CPS_trumppolls_final, clear

****************************************************************************
** Generate Parameters by Week
****************************************************************************

**Generate "bandwidth" and reference period 
global ftr = 14 // how many weeks after policy 
global bfr = 14 // how many weeks before policy
global cut = $ftr + $bfr
global pre = 2 // reference period?
local add = 5

**Generate weeks since first Covid; weeks since policy
rename first_covid_date firstcase
gen weeks_since_firstcase = round(((start_date - firstcase) / 7), 1)
gen weeks_since_lock = round(((start_date - closed_business) / 7), 1)
gen week = week(start_date)

****************************************************************************
** Essential Workers Median
****************************************************************************

quietly: sum popestimate2019, detail
gen median_pop = r(p50)

gen high_pop = 0 
replace high_pop = 1 if popestimate2019 >= median_pop

gen low_pop = 0 
replace low_pop = 1 if popestimate2019 < median_pop

****************************************************************************
** Truncate Sample for common support, etc.
****************************************************************************

drop if weeks_since_lock > $ftr & !missing(weeks_since_lock) // cut sample short to have common support
sum start_date if weeks_since_lock == $ftr
drop if start_date > r(max) // cut off untreated units after max date
drop if weeks_since_lock < (-$bfr - `add') & !missing(weeks_since_lock)
quietly: sum start_date if weeks_since_lock == (-$bfr - `add')
drop if start_date < r(min) // cut off untreated units before min date (start_date)

****************************************************************************
** Create sDiD parameters
****************************************************************************

replace weeks_since_lock = weeks_since_lock + $bfr 

// Create SDiD terms
foreach w of numlist 0/$cut {
	gen ago`w' = 0
	replace ago`w' = 1 if weeks_since_lock == `w'
}

quietly: sum weeks_since_firstcase
global case = r(max) 

// Generate weeks_since_firstcase dummies to avoid multicollinearity issues
foreach w of numlist 0/$case {
	gen firstcase`w' = 0
	replace firstcase`w' = 1 if weeks_since_firstcase == `w'
}

****************************************************************************
** Binning dummies: Absorb periods < -bfr & > bfr
****************************************************************************

gen binning_pre = 0 
replace binning_pre = 1 if weeks_since_firstcase < -14

gen binning_post = 0 
replace binning_post = 1 if weeks_since_firstcase > 14


****************************************************************************
** Method 1: Event Study Estimation
****************************************************************************

global temp = $cut + 1
global groups = 2
mat M = J($temp, 2*$groups,.)

local count = 1

foreach v of varlist high_pop low_pop {  /// Code spits out the 2 regs for high and lowrisk

	// Split
	preserve 
	keep if `v' == 1

	// Name 
	local num = "Above"
	
	local bfr1 = $bfr - $pre -1
	local bfr2 = $bfr - ($pre -1)
	
	// Generate weeks_since_firstcase dummies to offset multicollinearity issues
	quietly: sum weeks_since_firstcase
	local weeksmax = r(max) 
	foreach i of numlist 0/`weeksmax'{
		gen case`i' = (weeks_since_firstcase == `i')
	}
	
	// Generate week FE
	quietly: sum week 
	local datemin = r(min) 
	local datemax = r(max) 
	foreach i of numlist `datemin'/`datemax' {
		gen dat`i' = (week ==`i')
	}
	
	// Event Study Estimation // reghdfe just allows for multi-way FE and clustering
	reghdfe pct ago0-ago$cut case0-case`weeksmax' dat`datemin'-dat`datemax' cumulative_cases after_lock after_emergency after_k12 after_mask, absorb(state) vce(cluster state week)

	// Store Results for Plotting
	mat cov = e(V)
	
	local col = (`count'-1)*2 +1 
	local col2 = `col' + 1

	foreach i of numlist 1/$temp { // cut + 1 cause theres 1 zero term
		local foo = `i' - 1
		//if !inrange(`foo',$bfr-$pre, $bfr-$pre ){
			mat M[`i',`col'] =  _b[ago`foo']
			mat M[`i',`col2'] = _se[ago`foo']
		//}
	}

	//mat M	replace M6 = M3 + M4 + 2*M5[1...,6] = M[1...,3] + M[1...,4] + 2*M[1...,5]
	
	restore
	local count = `count' + 1
}

// Generate weeks_since_firstcase dummies to offset multicollinearity issues
	quietly: sum weeks_since_firstcase
	local weeksmax = r(max) 
	foreach i of numlist 0/`weeksmax'{
		gen case`i' = (weeks_since_firstcase == `i')
	}
	
	// Generate week FE
	quietly: sum week 
	local datemin = r(min) 
	local datemax = r(max) 
	foreach i of numlist `datemin'/`datemax' {
		gen dat`i' = (week ==`i')
	}
	
****************************************************************************
** Plot Results
****************************************************************************

preserve
// Plot Split Figures Against Each Other
svmat M 

drop if _n > ($cut +1)
gen xax = _n - ($bfr +1)
gen xax2 = xax + 0.13 // shift confidence bands a bit so readers can distinguish

// Confidence Bands

local count = 1
local groups2 = $groups * 2

foreach i of numlist 1(2)`groups2' {
	
	local i2 = `i' + 1
	gen min`count' = M`i' - 1.96*M`i2'
	gen max`count' = M`i' + 1.96*M`i2'
	
	gen coeff`count' = M`i'
	
	local count = `count' + 1
}
	
	
/************ Plot Graphs (order: low_GDP, high_GDP) *****************/

graph drop _all
graph set window fontface "Arial"
set scheme s2color

//// 
twoway (scatter coeff2 xax, mcolor(red%70)  msymbol(o)) /// 
	(rcap max2 min2 xax, lcolor(red%70)) ///
	(line coeff2 xax, lcolor(red%70) lpattern(solid)) ///
	(scatter coeff1 xax2, mcolor(edkblue) msymbol(Oh)) ///
	(rcap max1 min1 xax2, lcolor(edkblue)) ///
	(line coeff1 xax2, lcolor(edkblue) lpattern(dashed) graphregion(color(white)) bgcolor(white) ///
	legend(off) ylabel(,labsize(small)) xlabel(,labsize(small)) ///
	ytitle("Difference-in-Differences Estimate", size(medsmall)) xsize(7) ysize(4) xlabel(-$bfr(1)$ftr) ///
	xtitle("Weeks Since Business Closure Mandate",size(medsmall)) ///
	xline(0, lc(black) lpattern(dash) lw(medthick))   ///
	yscale(range(-0.025, 0.25)) ylabel(-10(5)40) ///
	yline(0.0, lpattern(dash) lc(black) lw(medthin))), name(business_lock)
	
